﻿import kliment.math.Calc;
import kliment.geom.SimplePoint;
import kliment.utils._Object;
import kliment.utils._Type;
/**
 * ...
 * @author Kliment
 * @version 0.1.5
 */
class kliment.utils.Align {
	static private var __X:String = 'x';
	static private var __Y:String = 'y';
	static private var __WIDTH:String = 'width';
	static private var __HEIGHT:String = 'height';
	static public var round:Boolean = true;
	static public var roundFunction:Function = Math.round;
	
	static public function left(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __X), _getValue(space, __X));
	}
	
	static public function right(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __X), _getValue(space, __X) + _getValue(space, __WIDTH) - _getValue(target, __WIDTH)); 
	}
	
	static public function top(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __Y), _getValue(space, __Y)); 
	}
	
	static public function bottom(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __Y), _getValue(space, __Y) + _getValue(space, __HEIGHT) - _getValue(target, __HEIGHT)); 
	}
	
	static public function topLeft(target:Object, space:Object):SimplePoint {
		return new SimplePoint(left(target, space), top(target, space));
	}
	
	static public function topRight(target:Object, space:Object):SimplePoint {
		return new SimplePoint(right(target, space), top(target, space));
	}
	
	static public function bottomRight(target:Object, space:Object):SimplePoint {
		return new SimplePoint(right(target, space), bottom(target, space));
	}
	
	static public function bottomLeft(target:Object, space:Object):SimplePoint {
		return new SimplePoint(left(target, space), bottom(target, space));
	}
	
	static public function center(target:Object, space:Object):SimplePoint {
		return new SimplePoint(centerHorizontal(target, space), centerVertical(target, space));
	}
	
	static public function centerTop(target:Object, space:Object):SimplePoint {
		return new SimplePoint(centerHorizontal(target, space), top(target, space));
	}
	
	static public function centerRight(target:Object, space:Object):SimplePoint {
		return new SimplePoint(centerVertical(target, space), right(target, space));
	}
	
	static public function centerBottom(target:Object, space:Object):SimplePoint {
		return new SimplePoint(centerHorizontal(target, space), bottom(target, space));
	}
	
	static public function centerLeft(target:Object, space:Object):SimplePoint {
		return new SimplePoint(centerVertical(target, space), left(target, space));
	}
	
	static public function centerHorizontal(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __X), _getValue(space, __X) + (Calc.half(_getValue(space, __WIDTH)) - Calc.half(_getValue(target, __WIDTH)))); 
	}
	
	static public function centerVertical(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __Y), _getValue(space, __Y) + (Calc.half(_getValue(space, __HEIGHT)) - Calc.half(_getValue(target, __HEIGHT)))); 
	}
	
	static public function matchWidth(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __WIDTH), _getValue(space, __WIDTH));
	}
	
	static public function matchHeight(target:Object, space:Object):Number {
		return _setProperty(target, _getPropertyName(target, __HEIGHT), _getValue(space, __HEIGHT));
	}
	
	static public function moreTop(target:Object, space:Object, interval:Number):Number {
		interval = interval || 0;
		return _setProperty(target, _getPropertyName(target, __Y), _getValue(space, __Y) - _getValue(target, __HEIGHT) - interval);
	}
	
	static public function moreRight(target:Object, space:Object, interval:Number):Number {
		interval = interval || 0;
		return _setProperty(target, _getPropertyName(target, __X), _getValue(space, __X) + _getValue(space, __WIDTH) + interval);
	}
	
	static public function moreBottom(target:Object, space:Object, interval:Number):Number {
		interval = interval || 0;
		return _setProperty(target, _getPropertyName(target, __Y), _getValue(space, __Y) + _getValue(space, __HEIGHT) +interval);
	}
	
	static public function moreLeft(target:Object, space:Object, interval:Number):Number {
		interval = interval || 0;
		return _setProperty(target, _getPropertyName(target, __X), _getValue(space, __X) - _getValue(target, __WIDTH) - interval);
	}
	
	static public function centerMoreTop(target:Object, space:Object):SimplePoint {
		return new SimplePoint(centerHorizontal(target, space), moreTop(target, space));
	}
	
	static public function centerMoreRight(target:Object, space:Object):SimplePoint {
		return new SimplePoint(moreRight(target, space), centerVertical(target, space));
	}
	
	static public function centerMoreBottom(target:Object, space:Object):SimplePoint {
		return new SimplePoint(centerHorizontal(target, space), moreBottom(target, space));
	}
	
	static public function centerMoreLeft(target:Object, space:Object):SimplePoint {
		return new SimplePoint(moreLeft(target, space), centerVertical(target, space));
	}
	
	static private function _getPropertyName(target:Object, property:String):String {
		return (_Type.isMovieClip(target) || target instanceof TextField)?('_'+property):property;
	}
	
	static private function _setProperty(target:Object, property:String, value:Number):Number {
		if (round)
			value = roundFunction(value);
		return Number(_Object.setProperty(target, property, value));
	}
	
	static private function _getValue(target:Object, property:String):Number {
		return Number(target[_getPropertyName(target, property)]);
	}
}